package myDaos;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.entities.ChubConsInvoiceInfo;
import model.entities.SalesMatchingDayInfo;

@Stateless
public class ChubInvoicesDao {
	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	public Boolean callChubInvoicesProcedure(String p_region,String p_country,String p_distributor_no,Date p_from_date,Date p_to_date){
		Boolean isCalled;
		try {
		    Query q = getEntityManager().createNativeQuery("{call CHUB_CONS_INVOICE_REPORT(:region, :country, :distributor, :from_date, :to_date)}").setParameter("region",p_region).setParameter("country",p_country).setParameter("distributor",p_distributor_no).setParameter("from_date",p_from_date).setParameter("to_date",p_to_date);
		    q.executeUpdate();
		    isCalled=true;
	} catch (Exception e) {
		e.printStackTrace();
		isCalled=false;
	}
		return isCalled;
	}
	
	public List<ChubConsInvoiceInfo> getChubInvoicesList(){
		List<ChubConsInvoiceInfo> ilist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT c FROM ChubConsInvoiceInfo c where 1=1"); 
			
			Query q = getEntityManager().createQuery(queryBiffer.toString());
			ilist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ilist;
	}

}
